import com.dinmax.flash.gsa.communication.parameter.*
import com.dinmax.flash.gsa.sgs.bridge.slot.*
import com.dinmax.flash.utilities.math.MathUtils;
import mx.services.SOAPParameter;

class MACHODROMO {

	private static var COUNT:Number = 0;
	private static var MAX:Number;
	private static var RANDOMA:Number;
	private static var RANDOMB:Number;
	private static var WINNERS:Array = new Array();
	private static var WIN:Boolean;
	
	public static function getLooserGame():Parameter {
		var parameter:Parameter;
		while(!parameter){
			parameter = randomLooser();
		}
		return parameter;
	}
	
	private static function randomLooser():Parameter {
		var nPos1:Number = MathUtils.randomRange(1, 6);
		var nPos2:Number = MathUtils.randomRange(1, 6);
		var nPos3:Number = MathUtils.randomRange(1, 6);
		var game = new Parameter();
		
		for(var i:Number=0; i < WINNERS.length; i++){
			var p = WINNERS[i];
			if(nPos1 == p.getItem("spin1").getValue() && nPos2 == p.getItem("spin2").getValue() && nPos3 == p.getItem("spin3").getValue()){
				return undefined;
			}
		}
		
		game.addItem("spin1", nPos1);
		game.addItem("spin2", nPos2);
		game.addItem("spin3", nPos3);
		return game;
	}
	
	public static function getWinnerGame():Parameter {
		return WINNERS[MathUtils.randomRange(0, WINNERS.length-1)];
	}
	
	public static function play(winner:Boolean):Void {
		COUNT++;
		WIN = winner || COUNT == MAX;
		if(WIN){
			COUNT = 0;
			var game = getWinnerGame();
		} else {
			var game = getLooserGame();
		}
		SLOTBridgeDummy.getActive().GAMESLOT_2_SWF_setPosition(game);
		
	}
	
	public static function roundEnd():Void {
		if(WIN){
			WIN = false;
			SLOTBridgeDummy.getActive().GAMESLOT_2_SWF_win(1);
		}
	}
	
	public static function init():Void {
		loadWinners();
		_root.onMouseDown = function(){
			if(_root.palanca.enabled){
				_root.palanca.onPress();
			}
		}
	}
	
	private static function loadWinners():Void{
		var lv:LoadVars = new LoadVars();
		lv.load("winners.txt");
		lv.onLoad = function(success:Boolean):Void {
			var winners:Array = this.values.split(",");
			for(var i:Number=0; i < winners.length; i++){
				var game:Array = winners[i].split("-");
				var parameter:Parameter = new Parameter();
				parameter.addItem("spin1", game[0]);
				parameter.addItem("spin2", game[1]);
				parameter.addItem("spin3", game[2]);
				MACHODROMO.WINNERS.push(parameter);
			}
			trace(MACHODROMO.WINNERS);
			MACHODROMO.loadConfig();
		}
	}
	
	public static function loadConfig():Void {
		var lv:LoadVars = new LoadVars();
		lv.load("config.txt");
		lv.onLoad = function(success:Boolean):Void {
			if(this.playRate2){
				MACHODROMO.MAX = MathUtils.randomRange(this.playRate, this.playRate2);
			} else {
				MACHODROMO.MAX = this.playRate;
			}
			var winners:Array = this.winners
			
			var sInit:Array = String(this.initPos).split(",");
			var parameter:Parameter = new Parameter();
			parameter.addItem("spin1", sInit[0]);
			parameter.addItem("spin2", sInit[1]);
			parameter.addItem("spin3", sInit[2]);
			SLOTBridgeDummy.getActive().GAMESLOT_2_SWF_setPosition(parameter, true);
		}
	}

}